package com.accelstack.cmp.service;

import com.accelstack.cmp.entity.VirtualMachine;
import com.accelstack.cmp.enums.VmStatus;
import com.accelstack.cmp.repository.VirtualMachineRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 虚拟机Service
 */
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class VirtualMachineService {
    
    private final VirtualMachineRepository vmRepository;
    
    @Transactional
    public VirtualMachine create(VirtualMachine vm) {
        return vmRepository.save(vm);
    }
    
    @Transactional
    public VirtualMachine update(VirtualMachine vm) {
        return vmRepository.save(vm);
    }
    
    @Transactional
    public void deleteById(Long id) {
        vmRepository.deleteById(id);
    }
    
    public Optional<VirtualMachine> findById(Long id) {
        return vmRepository.findById(id);
    }
    
    public List<VirtualMachine> findAll() {
        return vmRepository.findAll();
    }
    
    public Page<VirtualMachine> findAll(Pageable pageable) {
        return vmRepository.findAll(pageable);
    }
    
    public List<VirtualMachine> findByVirtHostId(Long virtHostId) {
        return vmRepository.findByVirtHostId(virtHostId);
    }
    
    public List<VirtualMachine> findByStatus(VmStatus status) {
        return vmRepository.findByStatus(status);
    }
}

